package com.dl.controller;

import com.dl.pojo.Page;
import com.dl.pojo.User;
import com.dl.service.UserService;
import com.dl.utils.ShiroUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author DL
 * @create 2021-03-16 14:36
 */
@RequiresRoles(logical = Logical.OR,value = {"admin","junior"})
@Controller
@Api(tags="用户操作接口")
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @GetMapping("/{str}")
    public String toStr(@PathVariable String str){
        return "user/" + str;
    }

    @ApiOperation("跳转用户管理首页")
    @GetMapping("/userInfo")
    public String userInfo(){
        return "user/user";
    }

    @ApiOperation(value="清空授权缓存",notes="清空授权缓存，下一次查询数据库")
    @ResponseBody
    @GetMapping(value = {"/clearAuthorizationCache","/clearAuthorizationCache/{username}"})
    public boolean clearAuthorizationCache(@PathVariable(required = false) String username) {
        System.out.println("username = " + username);
        return ShiroUtils.clearAuthorizationCache(username);
    }

    @ApiOperation(value="清空认证缓存",notes="清空认证缓存，下一次查询数据库")
    @ResponseBody
    @GetMapping(value = {"/clearAuthenticationCache","/clearAuthenticationCache/{username}"})
    public boolean clearAuthenticationCache(@PathVariable(required = false) String username) {
        System.out.println("username = " + username);
        return ShiroUtils.clearAuthenticationCache(username);
    }

    @ApiOperation("传入id删除用户")
    @ResponseBody
    @GetMapping("/del/{id}")
    public boolean del(@ApiParam(value = "用户id",example = "1") @PathVariable int id) {
        System.out.println("del=>" + id);
        return userService.deleteUser(id);
    }

    @ApiOperation("修改用户信息")
    @ResponseBody
    @PostMapping("/update")
    public boolean update(@RequestBody User user) {
        System.out.println("update=>" + user);
        return userService.updateUser(user);
    }

    @ApiOperation("修改用户状态信息")
    @ResponseBody
    @GetMapping("/updateStatus/{id}/{status}")
    public boolean updateStatusByID(@PathVariable int id, @PathVariable int status) {
        return userService.updateStatusByID(id, status);
    }

    @ApiOperation("查询用户，返回json")
    @ResponseBody
    @GetMapping("/find/{id}")
    public User findUserByID(@PathVariable int id){
        return userService.queryUserById(id);
    }

    @ApiOperation("ajax分页查询用户，返回json")
    @ResponseBody
    @GetMapping(value = {"/json","/json/{currentPage}"})
    public Page<User> queryUserByLimit(@PathVariable(required = false) Integer currentPage){
        //如果为空则是第一次访问，默认访问第一页
        if (currentPage == null) {
            currentPage = 1;
        }
        Page<User> page = userService.queryUserByLimit(currentPage, 10);
        page.setCurrentPage(currentPage);
        return page;
    }

    @ApiOperation("关键字查询，返回json")
    @ResponseBody
    @GetMapping(value = {"/search/{keyWord}","/search/{keyWord}/{currentPage}"})
    public Page<User> queryUserByKey(@PathVariable String keyWord, @PathVariable(required = false) Integer currentPage){
        //如果为空则是第一次访问，默认访问第一页
        if (currentPage == null) {
            currentPage = 1;
        }
        System.out.println("keyWord = " + keyWord);
        System.out.println("currentPage=>" + currentPage);
        Page<User> page = userService.queryUserByKey(keyWord, currentPage, 10);
        if (page != null) {
            page.setCurrentPage(currentPage);
            return page;
        }else {
            return null;
        }
    }
}
